<?php

class Cron extends Controller {

	function index(){ }

	function kurs_old()
	{ 
		global $VALUTA;
		$d1 = '01';
		$m1 = '01';
		$y1 = '2011';
		$d2 = date("d");
		$m2 = date("m");
		$y2 = date('Y');
		foreach($VALUTA as $k => $v)
		{
			if($k == 'R') continue;
			$ticket = $v;
			$url = 'http://export.rbc.ru/free/cb.0/free.fcgi?period=DAILY&tickers=' .$ticket. '&d1=' .$d1. '&m1=' .$m1. '&y1=' .$y1. '&d2=' .$d2. '&m2=' .$m2. '&y2=' .$y2. '&separator=TAB&data_format=BROWSER';
			$text = @file_get_contents($url);
			if($text)
			{
				$rows = explode("\n", $text);
				foreach($rows as $row)
				{
					$data = explode("\t", $row);
					if(@$data[5])
					{
						$data[5] = round($data[5] / intval($data[8]), 4);
						$this->MKurs->addExchange($k, $data[1], $data[5]);
					}
				}
			}
		}
	}

	function kurs()
	{ 
		$d2 = date("d");
		$m2 = date("m");
		$y2 = date('Y');
		$d1 = min(intval($d2), 28);
		$m1 = intval($m2) - 1;
		$y1 = intval($y2);
		if($m1 == 0)
		{
			$m1 = 12;
			$y1--;
		}
		$d1 = ($d1 < 10 ? "0" : '') .$d1;
		$m1 = ($m1 < 10 ? "0" : '') .$m1;
		$url = 'http://export.rbc.ru/free/cb.0/free.fcgi?period=DAILY&tickers=NULL&d1=' .$d1. '&m1=' .$m1. '&y1=' .$y1. '&d2=' .$d2. '&m2=' .$m2. '&y2=' .$y2. '&separator=TAB&data_format=BROWSER';
		$text = @file_get_contents($url);
		if($text)
		{
			$rows = explode("\n", $text);
			foreach($rows as $row)
			{
				$data = explode("\t", $row);
				if(@$data[5])
				{
					$this->MKurs->addExchange($data[0], $data[1], $data[5], $data[8]);
				}
			}
		}
	}

	function mavro()
	{
		$text = @file_get_contents('http://sergey-mavrodi.com/kurs.php');
		$text = substr($text, strpos($text, "</table>"));
		$text = substr($text, strpos($text, "<tr align=\"center\">"));
		$text = str_replace("\t", "", $text);
		$text = str_replace("<a name='current_day'></a>", "", $text);
		$text = str_replace(' bgcolor="#FFCC99"', "", $text);
		$text = str_replace(' bgcolor="#FFFFCC"', "", $text);
		$text = str_replace(' bgcolor="#EEEEEE"', "", $text);
		$text = str_replace(' bgcolor="#FFA602"', "", $text);
		$text = str_replace(' style="width:40px;"', "", $text);
		$text = str_replace(' style="width:35px;"', "", $text);
		$text = str_replace(' style="width:41px;"', "", $text);
		$text = str_replace(' style="width:50px;"', "", $text);
		$text = str_replace(' style="width:42px;"', "", $text);
		$text = str_replace(' style="width:51px;"', "", $text);
		$text = str_replace('<b>', "", $text);
		$text = str_replace('</b>', "", $text);
		$text = str_replace('<td>', "", $text);
		$text = str_replace('</td>', "", $text);
		$text = str_replace('</tr>', "", $text);
		$text = str_replace('<br />', ".", $text);
		$text = str_replace('Чт.', "", $text);
		$text = str_replace('Вт.', "", $text);
		$text = str_replace('<span style=\'font-size:14px\'>ном.</span>', "", $text);
		$arr_text = explode("\n", $text);
		$begin = true;
		$percent = 20;
		foreach($arr_text as $row)
		{
			$row = trim($row);
			if($row == '') continue;
			if(substr($row, 0, 8) == '</tbody>') continue;
			if(substr($row, 0, 3) == '<tr')
			{
				$begin = true;
				continue;
			}
			if($begin)
			{
				list($d, $m, $y) = explode(".", $row);
				$date = dateToDb($y."-".$m.'-'.$d);
				$begin = false;
				$percent = 20;
				continue;
			}
			list($buy, $sell) = explode("<hr>", $row);
			$buy = trim(str_replace(",", "", $buy));
			$sell = trim(str_replace(",", "", $sell));
			$arr = array(
				'date' => $date,
				'percent' => $percent,
				'buy' => $buy,
				'sell' => $sell
			);
			$percent += 10;
			if($percent == 70) 
			{
				$percent = 45;
			}
			$this->MKurs->updateKurs($arr);
		}
	
	}



}


?>